package com.iafme.config;

import cn.hutool.core.util.StrUtil;
import com.iafme.constants.CodeConstants;
import com.iafme.dto.req.RedemptionReqDTO;
import com.iafme.service.RedemptionService;
import com.systemframework.constants.Constants;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/14 14:29
 */
@Configuration
@Slf4j

public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    public RedisKeyExpirationListener(RedisMessageListenerContainer redisMessageListenerContainer) {
        super(redisMessageListenerContainer);
    }

    private RedemptionService redemptionService;

    @Autowired
    public void setRedemptionService(RedemptionService redemptionService) {
        this.redemptionService = redemptionService;
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String string = message.toString();
        log.info("过期redis----->key:{}",string);
        if(string.contains(CodeConstants.REDEMPTION_NUM_PREFIX)){
           String redemptionCode= StrUtil.subAfter(string,CodeConstants.REDEMPTION_NUM_PREFIX,true);
           RedemptionReqDTO reqDTO=new RedemptionReqDTO();
           reqDTO.setCode(redemptionCode);
           reqDTO.setStatus(Constants.TWO);
           redemptionService.updateRedemptionStatus(reqDTO);
         }

        super.onMessage(message, pattern);
    }


}
